<?php

namespace App\Http\Middleware;

use Closure;
use EasyWeChat\Foundation\Application;
use Illuminate\Support\Facades\Session;

class GetWxUserInfo
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!Session::has('wxCallBackUserInfo')) {
            //缓存原始请求的URL
            session()->put('wxURL', $request->url());
            $config   = config('wx_2');
            $app      = new Application($config);
            $response = $app->oauth->scopes(['snsapi_userinfo'])->redirect();
            return $response;
        }
        $response = $next($request);
        return $response;
    }
}
